package actions;

import java.util.ArrayList;
import java.util.List;

import javax.naming.InitialContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import controller.EmployeeDAORemote;
import entities.*;
import forms.EmpActionForm;

public class EmpAction extends Action {

	public List<Employee> getAllEmp() {
		List<Employee> list = new ArrayList<Employee>();
		return list;
	}

	public String creatEmp(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response)
	// TODO Auto-generated method stub

	{
		EmployeeDAORemote remote = lookup();

		EmpActionForm f = (EmpActionForm) form;

		int idEmployee = f.getIdEmployee();
		String idUser = f.getIdUser();
		String password = f.getPassword();
		String firstName = f.getFirstName();
		String lastName = f.getLastName();
		String emgContact = f.getEmgContact();
		String address = f.getAddress();

		String msg = "";

		msg = remote.creatEmp(idEmployee, idUser, password, firstName, lastName, emgContact, address);

		return msg;

	}

	public String updateEmp() {
		return "mess";
	}

	public String deleteEmp() {
		return "mess";
	}

	public EmployeeDAORemote lookup() {
		EmployeeDAORemote remote1 = null;
		try {
			InitialContext ctx = new InitialContext();
			remote1 = (EmployeeDAORemote) ctx
					.lookup("java:global/GoodHealth/GoodHealth-ejb/EmployeeDAO!controller.EmployeeDAORemote");
			System.out.print(remote1);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return remote1;

	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String act = request.getParameter("act");
		String msg = "";
		if (act.equals("create"))
			msg = creatEmp(mapping, form, request, response);

		if (msg.equals("success"))
			return mapping.findForward("success");
		else
			return mapping.findForward("fail");

	}

}
